package com.neusoft.movie.v_movies.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.adpater.MoviesAdapter;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.fragment.MoviesFragment;
import com.neusoft.movie.v_movies.javabean.MoviesBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.UsersModel;

import java.util.List;

public class MyColMoviesActivity extends BaseActivity implements RetrofitListener {

    private RecyclerView recyclerView;
    private ImageView left;
    private int u_id;
    private Context mContext;
    private  MoviesAdapter adapter;

    @Override
    void initEvents() {

        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    void initViews() {
        setLayout(R.layout.activity_my_col_movies);

        left=(ImageView) findViewById(R.id.left_button);

        recyclerView = (RecyclerView) findViewById(R.id.movies_list);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(1);
        recyclerView.addItemDecoration(decoration);

    }

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;
        public SpacesItemDecoration(int space) {
            this.space=space;
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }
    @Override
    void initData() {
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id=sharedPreferences.getInt("u_id",0);

        mycolmovies();
    }

    void mycolmovies()
    {
        UsersModel usersModel = new UsersModel();
        usersModel.mycolmovies(u_id,3,this);
    }

        @Override
    public void onSuccess(Object o, int flag) {
        switch (flag) {
            case Constants.MYCOLLECTMOVIES:
                List<MoviesBean> list = (List<MoviesBean>) o;
                MoviesAdapter adapter = new MoviesAdapter(this, list);
                recyclerView.setAdapter(adapter);
                break;
        }
    }
    @Override
    public void onFailed() {
        Toast.makeText(MyColMoviesActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
    }

}

